package fr.upstlse.glre.client.espacededie.commun.depot;

import java.util.Collection;

import com.extjs.gxt.ui.client.Events;
import com.extjs.gxt.ui.client.event.BaseEvent;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.fx.Draggable;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.Info;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FormHandler;
import com.google.gwt.user.client.ui.FormPanel;
import com.google.gwt.user.client.ui.FormSubmitCompleteEvent;
import com.google.gwt.user.client.ui.FormSubmitEvent;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

import fr.upstlse.glre.client.RfgConstante;
import fr.upstlse.glre.client.utilitaires.ControlerUtil;
import fr.upstlse.glre.client.vo.depot.LibelleVOGWT;
import fr.upstlse.glre.client.widgets.fields.DepotFields;

public class Depot extends Composite {
	/**
	 * Panneau offrant un certain nombre de widgets. Il contient le panneau de
	 * contenu.
	 */
	ContentPanel depotPanel;

	/**
	 * Panneau FORM contenant le profil au format formulaire pour l'édition.
	 */
	FormPanel deposerPanel;
	
	private static Depot instance;
		
	public Depot() {
		createDepotPanel();
		createDeposerForm();

		depotPanel.add(deposerPanel);
		initWidget(depotPanel);
		instance = this;
	}
	
	private void createDepotPanel() {
		depotPanel = new ContentPanel();
		depotPanel.setAutoWidth(true);
		depotPanel.setWidth(500);
		depotPanel.setId(RfgConstante.DEPOT_PANEL_ID);
		depotPanel.setBorders(true);
		depotPanel.setCollapsible(true);
		depotPanel.setExpanded(false);

		// Draggable panel
		Draggable lDraggable = new Draggable(depotPanel, depotPanel
				.getHeader());

		depotPanel.setHeading(RfgConstante.DEPOT_PANEL_HEADER_TEXT);
		
		
	}

	public void createDeposerForm() {
		deposerPanel = new FormPanel();
		deposerPanel.setAction(GWT.getModuleBaseURL() + "upload");
		deposerPanel.setEncoding(FormPanel.ENCODING_MULTIPART);
		deposerPanel.setMethod(FormPanel.METHOD_POST);

		VerticalPanel lFormContent = new VerticalPanel();
		final DepotFields lDepotFields = new DepotFields(lFormContent);
		
		depotPanel.addListener(Events.BeforeExpand, new Listener<BaseEvent>() {

			public void handleEvent(BaseEvent be) {
				createLibelleListBox(lDepotFields);
			}
		});
		
		lFormContent.add(new Button(RfgConstante.DEPOSER_FIELD_SUBMIT_LABEL, new ClickListener() {
			public void onClick(Widget sender) {
				deposerPanel.submit();
			}
		}));

		deposerPanel.addFormHandler(new FormHandler() {
			public void onSubmit(FormSubmitEvent arg0) {
			}

			public void onSubmitComplete(FormSubmitCompleteEvent event) {
				Info.display("Information", "Ajout du document " + event.getResults(), "");
			}
		});
		
		deposerPanel.add(lFormContent);
	}
	
	private void createLibelleListBox(
			final DepotFields lDepotFields) {
		final MessageBox lAttente = MessageBox.wait("Chargement", "Chargement des libellés", "Veuillez patienter");
		Timer timer = new Timer(){

			@Override
			public void run() {
				AsyncCallback<Collection<LibelleVOGWT>> lCallback = new AsyncCallback<Collection<LibelleVOGWT>>() {
					
					public void onFailure(Throwable pCaught) {
						lAttente.hide();
						MessageBox.alert("Erreur getAllLibelle ", pCaught.getMessage(), null);
					}

					public void onSuccess(Collection<LibelleVOGWT> pResult) {
						lAttente.hide();
						Collection<LibelleVOGWT> lLibelleVOGWTs =  pResult;
						ListBox lLibelleListBox = lDepotFields.getLibelle().getListbox();
						for(LibelleVOGWT lLibelleVOGWT : lLibelleVOGWTs) {
							lLibelleListBox.addItem(lLibelleVOGWT.getIntitule(), new Long(lLibelleVOGWT.getId()).toString());
						}
						Info.display("Information", "Succès du chargement des libellés", "");
						lAttente.hide();
					}

					
				};
				 ControlerUtil.getControlerServiceAsyncInstance().getAllLibelles(lCallback);
			}
		};
		timer.schedule(5);
	}

	public static Depot getInstance() {
		if(instance == null) {
			instance = new Depot();
		}
		return instance;
	}

	public static void setInstance(Depot pInstance) {
		instance = pInstance;
	}

	public ContentPanel getDepotPanel() {
		return depotPanel;
	}

	public void setDepotPanel(ContentPanel depotPanel) {
		this.depotPanel = depotPanel;
	}

	public FormPanel getDeposerPanel() {
		return deposerPanel;
	}

	public void setDeposerPanel(FormPanel deposerPanel) {
		this.deposerPanel = deposerPanel;
	}
}
